package com.etc.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Application Lifecycle Listener implementation class MySessionAttrListener
 *
 */
@WebListener
public class MySessionAttrListener implements HttpSessionAttributeListener {

    /**
     * Default constructor. 
     */
    public MySessionAttrListener() {
        System.out.println("构造");
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0)  { 
        System.out.println("添加属性");
        //getname得到的结果是setAttribute的key
        String name = arg0.getName();
        System.out.println(name);
       
       //getvalue得到的结果是setAttribute的value
        Object value = arg0.getValue();
        System.out.println(value);
        
        //也可以直接通过name得到value
        HttpSession session = arg0.getSession();
        System.out.println("session:"+session.getAttribute(name)); 
        
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
    	System.out.println("删除属性");
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
    	System.out.println("修改属性");
    }
	
}
